﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Security.Cryptography;
using System.IO;

namespace ourForm
{
    public partial class ucEncryptSymAlg : UserControl
    {
        string receiverPublicKey;
        string FilePath;
        byte[] Key = null;
        byte[] IV = null;
        public ucEncryptSymAlg()
        {
            InitializeComponent();
            // this.dataGridView1.DataSource = receiverBUSBindingSource.DataSource;
           // this.receiverTableAdapter.FillBy(this._0612180_0612184_MaHoaDataSet.Receiver);

            ReceiverBUS bus = new ReceiverBUS();
            List<ReceiverDTO> lst = bus.GetAllReceiver();
            this.dataGridView1.DataSource = lst;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            string email = (string)this.dataGridView1.CurrentRow.Cells["email"].Value;
            string name = (string)this.dataGridView1.CurrentRow.Cells["name"].Value;
            txtEmail.Text = email;
            txtName.Text = name;
            receiverPublicKey = (string)this.dataGridView1.CurrentRow.Cells["publickey"].Value;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                FilePath = dlg.FileName;
                txtFilePath.Text = dlg.FileName;
            }
        }

        private int Cipher()
        {
            int cm = 1;
            if (radECB.Checked)
                cm = 1;
            if (radCBC.Checked)
                cm = 2;
            if (radCFB.Checked)
                cm = 3;
            if (radCTS.Checked)
                cm = 5;
            if (radOFB.Checked)
                cm = 4;
            return cm;
        }

        private int SymAlgo()
        {

            int SymAlgo = 0;
            // group radio SymAlgo
            if (radDES.Checked)
                SymAlgo = 1;
            if (radTripleDES.Checked)
                SymAlgo = 2;
            if (radRijndael.Checked)
                SymAlgo = 3;
            if (radRC2.Checked)
                SymAlgo = 4;
            return SymAlgo;

        }

        private int Padding()
        {
            int pm = 1;
            if (radPKCS7.Checked)
                pm = 1;
            if (radISO.Checked)
                pm = 2;
            if (radANSI.Checked)
                pm = 3;
            if (radZero.Checked)
                pm = 4;
            if (radNone.Checked)
                pm = 5;
            return pm;
        }

       
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            byte[] Key = null;
            byte[] IV = null;
            
            #region mã hóa file
            int SymAlgo = this.SymAlgo();
            int cm = this.Cipher();
            int pm = this.Padding();



            if (FilePath.Length > 0 && File.Exists(FilePath))
            {
                try
                {
                   
                    byte[] plainBytes = File.ReadAllBytes(FilePath);
                    byte[] cipherBytes = OurSymAlgo.Encrypt(plainBytes, ref Key,
                                                    ref IV, cm, pm, SymAlgo, receiverPublicKey);

                    using (Stream s = File.Create(FilePath + ".enc"))
                    {
                        s.Write(cipherBytes, 0, cipherBytes.Length);
                    }
                    MessageBox.Show("File Encrypted Successfully");

                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                MessageBox.Show("File not found!");
            }
            #endregion

           




        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void radOFB_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void radRijndael_CheckedChanged(object sender, EventArgs e)
        {
            if (radRijndael.Checked)
            {
                radZero.Enabled = radNone.Enabled = false;
            }
            else
            {
                radZero.Enabled = radNone.Enabled = true;
            }
        }

       

    }
}
